package com.itheima.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmeaIService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
   @Autowired
    DishService dishService;
   @Autowired
    SetmeaIService setmeaIService;

    /**
     * 根据id删除分类,删除之前要做判断
     * @param id
     */
    @Override
    public void remove(Long id) {
        //1.添加查询条件,根据分类id进行查询菜品数据
        QueryWrapper<Dish> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",id);
        int count = dishService.count(queryWrapper);
        //如果已关联就抛出一个业务异常
        if (count>0){
            throw new CustomerException("当前分类下关联的菜品不能删除");
        }//已关联菜品,抛出一个业务异常

        //2.查询当前分类是否关联了套餐,如果已关联,抛出一个业务异常
        QueryWrapper<Setmeal> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("category_id",id);
        int count1 = setmeaIService.count(queryWrapper1);
         //如果已关联就抛出一个业务异常
        if (count1>0){
            throw new CustomerException("当前分类下关联的套餐,不能删除");
        }//已关联菜品,抛出一个业务异常

        //根据id删除
        this.removeById(id);
    }
}
